## SPDX-License-Identifier: MIT

import requests
import json

url = 'https://www.bjbus.com/api/api_etaline_list.php?hidden_MapTool=busex2.BusInfo&city=%u5317%u4EAC&pageindex=1&pagesize=30&fromuser=bjbus&datasource=bjbus&clientid=9db0f8fcb62eb46c&webapp=mobilewebapp&what={}'
token = 'eyJhbGciOiJIUzI1NiIsIlR5cGUiOiJKd3QiLCJ0eXAiOiJKV1QifQ.eyJwYXNzd29yZCI6IjY0ODU5MTQzNSIsInVzZXJOYW1lIjoiYmpidXMiLCJleHAiOjE2ODQwNDgxNjZ9.0zgvMMgwk5Gr9XSt3om8hys1ZtYCGe2QB1nPlugbWao'

def search_routes(number):
    resp = requests.get(url.format(number))
    if resp.status_code != 200:
        raise
    j = json.loads(resp.content)
    result_set = j['response']['resultset']
    if len(result_set) > 0:
        return result_set['data']['feature']
    else:
        return []

def search_routes_exact(number):
    number_s = str(number)
    return list(filter(lambda x: x['lineName'] == number_s, search_routes(number_s)))

def show_line_info(lineId):
    url_sel = 'https://www.bjbus.com/api/api_etaline.php?lineId={}&pageNum=1&token=' + token
    resp = requests.post(url_sel.format(lineId))
    j = json.loads(resp.content)
    return j['data']

def show_stops(lineId):
    url_eta = 'http://www.bjbus.com/api/api_etastation.php?lineId='+lineId+'&token=' + token
    resp = requests.post(url_eta)
    j = json.loads(resp.content)
    return j['data']

def show_eta(lineId,stopId):
    url_eta = 'http://www.bjbus.com/api/api_etartime.php?conditionstr='+lineId+'-'+stopId+'&token=' + token
    resp = requests.post(url_eta)
    j = json.loads(resp.content)
    return j['data']
